package com.foo.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.foo.servicesCall.EventWSClient;
import com.foo.servicesCall.RestaurantWSClient;
import com.foo.servicesCall.EventsStub.Event;
import com.foo.servicesCall.EventsStub.EventsArray;
import com.foo.servicesCall.RestaurantServiceStub.Restaurant;
import com.foo.servicesCall.RestaurantServiceStub.RestaurantList;

/**
 * Servlet implementation class EventServlet
 */
public class EventServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		RequestDispatcher dispatcher;
		HttpSession session = request.getSession(true);
		EventsArray eventsArray = null;
		
		session.setAttribute("EventsArrayData", null);
		EventWSClient client = new EventWSClient();
		
		String sortBased = request.getParameter("SortBased");
		
		if(sortBased.equalsIgnoreCase("allevents"))
		{
			eventsArray = client.getAllEvents();
		}
		else if(sortBased.equalsIgnoreCase("eventsbykeyword"))
		{
			String keyword = request.getParameter("eventsbykeyword");
			eventsArray = client.getEventsByKeyword(keyword);
		}
		else if(sortBased.equalsIgnoreCase("eventsbydate"))
		{
			String start = request.getParameter("fromDate");
			String end = request.getParameter("toDate");
			eventsArray = client.getEventsByDate(start, end);
		}
		
		Event[] events =null;
		if(eventsArray!=null)
			{
			events = eventsArray.getEvent();
			}
		
		session.setAttribute("EventsArrayData", events);
		dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
		dispatcher.forward(request, response);	      
	}
	
}
